<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="城市区域" prop="cityName">
                <a-select
                  placeholder="所在城市"
                  v-model="queryParam.cityName"
                  style="width: 100%"
                  allow-clear
                >
                  <a-select-option
                    v-for="(d, index) in cityOptions"
                    :key="index"
                    :value="d.name.replace('市', '')"
                    >{{ d.name }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-customer-filter-add-form
        v-if="showAddModal"
        ref="bizCustomerFilterAddForm"
        :carStatusOptions="carStatusOptions"
        :levelOptions="levelOptions"
        :progressOptions="progressOptions"
        :cityOptions="cityOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-customer-filter-edit-form
        v-if="showEditModal"
        ref="bizCustomerFilterEditForm"
        :carStatusOptions="carStatusOptions"
        :levelOptions="levelOptions"
        :progressOptions="progressOptions"
        :cityOptions="cityOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <advance-table
        title="筛选客户条件"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
        }"
        tableKey="base-bizCustomerFilter-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="bizCustomerFilterList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{
          columnWidth: '5%',
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      >
        <div class="table-operations" slot="button">
          <a-button
            type="primary"
            @click="handleAdd"
            v-hasPermi="['biz:bizCustomerFilter:add']"
          >
            <a-icon type="plus" />新增
          </a-button>

          <a-button
            type="danger"
            v-if="!multiple"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:bizCustomerFilter:remove']"
          >
            <a-icon type="delete" />删除
          </a-button>
        </div>

        <span slot="pgPrice" slot-scope="{ text }"> {{ text }}万</span>

        <span slot="carStatus" slot-scope="{ record }">
          {{ carStatusFormat(record) }}
        </span>

        <span slot="level" slot-scope="{ record }">
          {{ levelFormat(record) }}
        </span>

        <span slot="progress" slot-scope="{ record }">
          {{ progressFormat(record) }}
        </span>

        <span slot="operation" slot-scope="{ text, record }">
          <a
            @click="handleUpdate(record)"
            v-hasPermi="['biz:bizCustomerFilter:edit']"
          >
            修改
          </a>
          <a-divider
            type="vertical"
            v-hasPermi="['biz:bizCustomerFilter:remove']"
          />
          <a
            @click="handleDelete(record)"
            v-hasPermi="['biz:bizCustomerFilter:remove']"
          >
            删除
          </a>
        </span>
        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import {
  listBizCustomerFilter,
  delBizCustomerFilter,
  exportBizCustomerFilter,
} from "@/api/biz/bizCustomerFilter";
import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizCustomerFilterAddForm from "@/views/biz/customerfilter/modules/BizCustomerFilterAddForm";
import BizCustomerFilterEditForm from "@/views/biz/customerfilter/modules/BizCustomerFilterEditForm";
import { findListByCode } from "@/api/biz/sysArea";
export default {
  name: "BizCustomerFilter",
  components: {
    AdvanceTable,
    BizCustomerFilterAddForm,
    BizCustomerFilterEditForm,
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 筛选客户条件表格数据
      bizCustomerFilterList: [],
      // 车辆状态字典
      carStatusOptions: [],
      // 重要级别字典
      levelOptions: [],
      // 进度字典
      progressOptions: [],
      // 城市列表
      cityOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        cityName: undefined,
      },
      columns: [
        {
          title: "序号",
          dataIndex: "rowindex",
          ellipsis: true,
          align: "center",
          width: "5%",
          scopedSlots: { customRender: "rowindex" },
        },
        {
          title: "城市名称",
          dataIndex: "cityName",
          ellipsis: true,
          align: "center",
          width: "10%",
        },
        {
          title: "车辆状态",
          dataIndex: "carStatus",
          scopedSlots: { customRender: "carStatus" },
          align: "center",
          width: "10%",
        },

        {
          title: "跟进状态",
          dataIndex: "progress",
          scopedSlots: { customRender: "progress" },
          align: "center",
          width: "10%",
        },
        {
          title: "最低评估价格",
          dataIndex: "pgPrice",
          align: "right",
          width: "10%",
          scopedSlots: { customRender: "pgPrice" },
        },
        {
          title: "最低星级",
          dataIndex: "level",
          scopedSlots: { customRender: "level" },
          align: "center",
          width: "10%",
        },
        {
          title: "每日限制次数",
          dataIndex: "maxCount",
          align: "right",
          width: "10%",
        },
        {
          title: "已分配次数",
          dataIndex: "shareCount",
          align: "right",
          width: "10%",
        },
        {
          title: "操作",
          dataIndex: "operation",
          align: "center",
          width: "10%",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDicts("car_status_options").then((response) => {
      this.carStatusOptions = response.data;
    });
    this.getDicts("level_options").then((response) => {
      this.levelOptions = response.data;
    });
    this.getDicts("bus_progress").then((response) => {
      this.progressOptions = response.data;
    });

    this.handleFindCityList();
  },
  methods: {
    // 获取城市区域列表
    handleFindCityList() {
      findListByCode().then((res) => {
        this.cityOptions = res.data;
      });
    },
    /** 查询筛选客户条件列表 */
    getList() {
      this.loading = true;
      listBizCustomerFilter(this.queryParam).then((response) => {
        this.bizCustomerFilterList = response.data.list;
        console.log("this.bizCustomerFilterList", this.bizCustomerFilterList);
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 车辆状态字典翻译
    carStatusFormat(row) {
      if (row.carStatus) {
        return this.selectDictLabels(
          this.carStatusOptions,
          this.ruleTextFormat(row.carStatus)
        );
      } else {
        return "";
      }
    },

    // 字符串处理:去掉中括号和双引号
    ruleTextFormat(text) {
      if (text && text.length > 0) {
        return text.replace("[", "").replace("]", "").replace(/"/g, "");
      } else {
        return "";
      }
    },

    // 重要级别字典翻译
    levelFormat(row) {
      if (row.level) {
        return this.selectDictLabels(this.levelOptions, row.level);
      } else {
        return "";
      }
    },
    // 进度字典翻译
    progressFormat(row) {
      if (row.progress) {
        return this.selectDictLabels(
          this.progressOptions,
          this.ruleTextFormat(row.progress)
        );
      } else {
        return "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        cityName: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd() {
      this.showAddModal = true;
      this.$nextTick(() => this.$refs.bizCustomerFilterAddForm.handleAdd());
    },
    handleUpdate(record, ids) {
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizCustomerFilterEditForm.handleUpdate(record, ids)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizCustomerFilterIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizCustomerFilter(bizCustomerFilterIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizCustomerFilter(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>
